<template>
  <div>
    <el-form :inline="true" :model="cform" class="demo-form-inline">
      <el-form-item label="车间">
        <CheJianSelect ref="cheJianS" @cheJianSelect="cheJianSelect"></CheJianSelect>
      </el-form-item>
      <el-form-item label="工区">
        <GongQuSelect ref="gongQuS" @gongQuSelect="gongQuSelect"></GongQuSelect>
      </el-form-item>
      <el-form-item label="请选择人员">
        <el-select  v-model="value" placeholder="请选择">
          <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">添加</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="id"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="phone" label="电话"></el-table-column>
      <el-table-column prop="cheJian" label="车间"></el-table-column>
      <el-table-column prop="gongQu" label="工区"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="deleteUser(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="page" background
                     :hide-on-single-page="true" :page-size="10" layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import CheJianSelect from "./CheJianSelect";
import GongQuSelect from "./GongQuSelect";
export default {
  name: "FangHuYuan",
  components: {GongQuSelect, CheJianSelect},
  data() {
    return {
      options: [],
      value: '',
      cform: {
        cheJianId:'',
        gongQuId:'',
      },
      tableData:[],
      page:1,
      total:0,
      type:'2'// 负责人
    }
  },
  created() {
    this.initTable()
  },
  methods: {
    async onSubmit() {
      const that = this;
      let url = 'api/user/addType?id='+that.value+"&type="+that.type
      let result = await this.$http.post(url)
      result = result.data
      if(result.code == 200){
        that.$message.success(result.message)
        that.value=''
        that.$refs.cheJianS.clear();
        that.$refs.gongQuS.clear();
        await that.initTable(that.page)
      }else{
        that.$message.error(result.message)
      }
    },
    async initSelect(id){
      const url = 'api/user/list?id='+id
      const that = this;
      let result = await this.$http.get(url)
      that.options = result.data.data.list
    },
    async initTable(){
      const that = this;
      const url = 'api/user/listTypePage?limit=10&page='+that.page+'&type='+that.type
      let result = await this.$http.get(url)
      that.total = result.data.data.length
      that.tableData = result.data.data.list
    }
    ,
    handleCurrentChange(val){
      this.initTable(val)
    },
    async deleteUser(row) {
      const url = 'api/user/deleteType?id='+row.id
      const that = this;
      let page = that.page
      let result = await this.$http.get(url)
      that.$message.success(result.data.message)
      await that.initTable(page)
    },
    cheJianSelect(val){
      this.$refs.gongQuS.clear();
      this.cform.cheJianId = val
      this.$refs.gongQuS.init(val);
    },
    gongQuSelect(val){
      this.cform.cheJianId = val
      this.value=''
      this.initSelect(val)
    },
  }
}
</script>

<style lang="less" scoped>

</style>
